const path = require('path');
const gulp = require('gulp');
const cssmin = require('gulp-cssmin');
const scss = require('gulp-sass');
const concat = require('gulp-concat');
const autoprefix = require('gulp-autoprefixer');
const babel = require('gulp-babel');
const uglify = require('gulp-uglify');
const webpack = require('webpack-stream');

gulp.task('css', function () {
    return gulp.src(path.resolve(__dirname, './public/static/admin/scss/*.scss'))
        .pipe(concat('common.css'))
        .pipe(autoprefix({
            overrideBrowserslist: [
                // "Android 4.1",
                // "iOS 7.1",
                "Chrome > 31",
                "ff > 31",
                "ie >= 8"
            ],
            grid: true
        }))
        .pipe(scss())
        .pipe(cssmin())
        .pipe(gulp.dest(path.resolve(__dirname, './public/static/admin/css')))
});

gulp.task('js', function () {
    return gulp.src(path.resolve(__dirname, './public/static/admin/es6/**.js'))
        .pipe(babel({
            presets: [
                '@babel/env'
            ],
            plugins: [
                '@babel/plugin-transform-runtime'
            ]
        }))
        .pipe(uglify())
        .pipe(webpack({
            output: {
                filename: 'common.js'
            },
            mode: 'production'
        }))
        .pipe(gulp.dest(path.resolve(__dirname, './public/static/admin/js/')))
});

gulp.task('build', function () {
    gulp.watch('./public/static/admin/scss/*.scss', gulp.series('css'));
    gulp.watch('./public/static/admin/es6/**.js', gulp.series('js'));
});

gulp.task('watch', gulp.series(['css', 'js', 'build']));
